<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->library('output');
        $this->output->nocache();
        
        // check if already logged in
        if ($this->session->userdata('logged_in_user'))
        {
            redirect('administrator/dashboard');
        }
    }

    /**
     * Display login form
     * @return void
     */
    public function index()
	{
        $page_info['title'] = 'Login :: Robi CMS';
        $page_info['url_suffix'] = $this->config->item('url_suffix');
        $page_info['message_error'] = '';

        if ($this->session->flashdata('message_error'))
        {
            $page_info['message_error'] = $this->session->flashdata('message_error');
        }
        
		$this->load->view('login', $page_info);
	}

    /**
     * Validate and Authenticate Username and Password then redirect to the dashboard
     * @return void
     */
    public function do_login()
    {
        $this->load->library('user');
        $this->load->library('session');

        // check authentication
        $username = $this->input->post('user_login_id');
        $password = $this->input->post('user_password');

        $logged_in_user = $this->user->check_username_password($username, $password);

        if($logged_in_user)
        {
            $this->session->set_userdata('logged_in_user', $logged_in_user);
            redirect('administrator/dashboard');
        }
        else
        {
            $this->session->set_flashdata('message_error', 'Authentication failed.');
            redirect('login');
        }
    }

    /**
     * Generate and send new password by email
     * @return void
     */
    public function generate_new_password()
    {

    }
}

/* End of file login.php */
/* Location: ./application/controllers/login.php */